package com.ciwong.epaper.modules.pad.homeworkwidget.presenter;


import android.content.Context;
import android.content.Intent;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.BasePaging;
import com.ciwong.epaper.modules.epaper.bean.MyGrade;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.bean.MyWork;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.i.BaseCallBack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.ciwong.epaper.modules.pad.contentprovider.MyGradeWidgetProvider.ACTION_RTFRESH_GRADE_SUCCESS;


public class DataProvider {

    private static String TAG = "DataProvider";//请求当前页
    private static int currentPage = 1;//请求当前页
    private static int loadPagerSize = 10;//请求当前页

    private static List<MyWork> myWorkList = new ArrayList<>();//作业列表

    public static List<MyWork> getHomeworkList() {
        return myWorkList;
    }


    public static void loadLocalHomeworks(final Context context) {
        SerializableManager.getInstance().deSerialize(SerializableManager.SerializeKey.SHARE_KEY_NOTIFICATION_LIST, new BaseCallBack() {
            @Override
            public void success(Object data) {
                if (data != null) {
                    List<MyWork> list = (List<MyWork>) data;
                    if (myWorkList == null)
                        myWorkList = new ArrayList<>();
                    myWorkList.clear();
                    myWorkList.addAll(list);
                }
                Intent intent = new Intent();
                intent.setAction(AppWidgetConstants.ACTION_LOAD_LOCAL_HOMEWORK_SUCCESS);
                context.sendBroadcast(intent);
            }

            @Override
            public void failed(int errorCode, Object data) {
                failed(data);
            }

            @Override
            public void failed(Object data) {
                getMyWorkList(context);
            }
        });

    }

    /**
     * 获取作业通知列表
     */
    public static void getMyWorkList(final Context context) {
        getMyWorkList(context, currentPage, loadPagerSize);
    }

    private static void getMyWorkList(final Context context, int currentPage, int loadPagerSize) {
        if (null == EApplication.getInstance().getUserInfoBase()) {
            return;
        }
        EpaperDao.getInstance().getMyWork(EApplication.BRAND_ID,0, currentPage, loadPagerSize, 0, false,false, new BaseExtCallBack(context, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void success(Object data) {
                BasePaging<MyWork> workNotifyBasePaging = (BasePaging<MyWork>) data;
                CopyOnWriteArrayList<MyWork> list = new CopyOnWriteArrayList<MyWork>(workNotifyBasePaging.getPageList());
                if (myWorkList == null)
                    myWorkList = new ArrayList<>();
                myWorkList.clear();
                myWorkList.addAll(list);
                SerializableManager.getInstance().serialize(SerializableManager.SerializeKey.SHARE_KEY_NOTIFICATION_LIST, (Serializable) list);
                Intent intent = new Intent();
                intent.setAction(AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_SUCCESS);
                context.sendBroadcast(intent);
//                ToastUtil.INSTANCE.toastCenterSuccess("刷新成功！");
            }

            @Override
            public void failed(int errorCode, Object data) {
                failed(data);
                super.failed(errorCode, data);
            }

            @Override
            public void failed(Object data) {
                CWLog.d(TAG, "lzh DataProvider 请求网络作业数据失败：" + data.toString());
                Intent intent = new Intent();
                intent.setAction(AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_FAILED);
                context.sendBroadcast(intent);
//                ToastUtil.INSTANCE.toastCenterError("刷新失败！");
            }
        });
    }

    public static void getmygrade(final Context context, String moduleIds) {
        if (null == EApplication.getInstance().getUserInfoBase()) {
            return;
        }
        EpaperDao.getInstance().getmygrade(EApplication.BRAND_ID, moduleIds, new BaseExtCallBack(context, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void success(Object data) {
                ArrayList<MyGrade> myGrades = (ArrayList<MyGrade>) data;
                CWLog.d("MyGrade=success", data.toString());
                Intent intent = new Intent();
                intent.setAction(ACTION_RTFRESH_GRADE_SUCCESS);
                intent.putExtra(IntentFlag.INTENT_FLAG_MYGRADE, myGrades);
                context.sendBroadcast(intent);
            }

            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                failed(data);
            }

            @Override
            public void failed(Object data) {
                CWLog.d("MyGrade=failed", data.toString());
            }
        });
    }


}
